﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq.Expressions;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class MainView : IUIView
{
    int index=0;
    int oldIndex = 0;
    public Button  quit,startBtn,SettingBtn,gameDesBtn;
   
    public override void Initinalize()
    {
       
        quit.onClick.AddListener(()=> { Application.Quit(); });
        startBtn.onClick.AddListener(StartGame);
        SettingBtn.onClick.AddListener(SettingGame);
        gameDesBtn.onClick.AddListener(GameDes);
        InitPointer();
    }

    private void GameDes()
    {
        Hide();
        UIManager.Instance.gameDesPanel.Show();
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            if (index == 3) return;
            oldIndex = index;
            index++;
            index = Mathf.Clamp(index,0,3);
            OnPointerChange(oldIndex, index);
        }
        else if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            if (index == 0) return;
            oldIndex = index;
            index--;
            index = Mathf.Clamp(index, 0, 3);
            OnPointerChange(oldIndex, index);
        }
        else if (Input.GetKeyDown(KeyCode.Return))
        {
            switch (index)
            {
                case 0:
                    StartGame();
                    break;
                case 1:
                    SettingGame();
                    break;
                case 2:
                    GameDes();
                    break;
                case 3:
                    Application.Quit();
                    break;
            }
        }
    }
    void OnPointerChange(int oldIndex,int Index)
    {
        switch (oldIndex)
        {
            case 0:
                startBtn.transform.GetChild(0).gameObject.SetActive(false);
                break;
            case 1:
                SettingBtn.transform.GetChild(0).gameObject.SetActive(false);
                break;
            case 2:
                gameDesBtn.transform.GetChild(0).gameObject.SetActive(false);
                break;
            case 3:
                quit.transform.GetChild(0).gameObject.SetActive(false);
                break;
        }
        switch (Index)
        {
            case 0:
                startBtn.transform.GetChild(0).gameObject.SetActive(true);
                break;
            case 1:
                SettingBtn.transform.GetChild(0).gameObject.SetActive(true);
                break;
            case 2:
                gameDesBtn.transform.GetChild(0).gameObject.SetActive(true);
                break;
            case 3:
                quit.transform.GetChild(0).gameObject.SetActive(true);
                break;
        }
       
    }

    public void StartGame()
    {
        Hide();
        UIManager.Instance.selectSavePanel.Show();
    }

    public void SettingGame()
    {
        Hide();
        UIManager.Instance.settingPanel.Show();
    }

    public override void Hide()
    {

        base.Hide();
    }
    public override void Show()
    {
        InitPointer();
        base.Show();
    }
    void InitPointer()
    {
        index = oldIndex = 0;
        startBtn.transform.GetChild(0).gameObject.SetActive(true);
        SettingBtn.transform.GetChild(0).gameObject.SetActive(false);
        quit.transform.GetChild(0).gameObject.SetActive(false);
        gameDesBtn.transform.GetChild(0).gameObject.SetActive(false);
    }
}
